/*
 * Created on 2005-8-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.jedi;

import java.util.ArrayList;
import java.util.List;

import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author dai
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class RoleMgr extends BaseObj {
	public RoleMgr() {}
	public Role get(String roleId,boolean isGetPower) {
		clearErr();
		Role role = new Role(roleId,isGetPower);
		DbConn conn = new DbConn();
		try {
			String sql = "select * from roleinfo where role_id = ?";
			conn.prepare(sql);
			conn.setString(1,roleId);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			role.setRoleName(get(rs,0,"role_name"));
			role.setShowOrder(getInt(rs,0,"show_order"));
			role.setRoleDesc(get(rs,0,"role_desc"));
			return role;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "select * from roleinfo where 1=1 ";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() > 0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Role role = new Role(get(rs,i,"role_id"));
				role.setRoleName(get(rs,i,"role_name"));
				role.setShowOrder(getInt(rs,i,"show_order"));
				role.setRoleDesc(get(rs,i,"role_desc"));
				r.add(role);
			}
			return r;
		}
		return null;
	}
}
